package main

import (
	"net/http"
	"fmt"
	"github.com/julienschmidt/httprouter"
	"streaming-web-go/api/session"
)
type middleWareHandler struct {
	r *httprouter.Router
}

func NewMiddleWareHandler(r *httprouter.Router) http.Handler {
	m := middleWareHandler{}
	m.r = r

	return m
}

func (m middleWareHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	//check session
	u := r.URL
	path := u.Path
	fmt.Printf("path : %v", path)
	validateUserSession(r)
	m.r.ServeHTTP(w, r)
}

func RegisterHandlers() *httprouter.Router {
	router := httprouter.New()
	router.POST("/user", CreateUser)
	router.POST("/user/:username", Login)

	router.GET("/user/:username",GetUserInfo)
	router.POST("/user/:username/videos", AddNewVideo)

	router.GET("/user/:username/video",ListAllVideos)

	router.DELETE("/user/:username/videos/:vid-id", DeleteVideo)
	router.POST("/videos/:vid-id/comments", PostComment)

	router.GET("/videos/:vid-id/comments",ShowComments)

	return router
}

func Prepare() {
	session.LoadSessionsFromDB()
}

func main() {
	Prepare()
	r := RegisterHandlers()
	mh := NewMiddleWareHandler(r)
	http.ListenAndServe(":8000",mh)

}